package com.live.video.server;

import com.live.video.mapper.UserMapper;
import com.live.video.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description:
 * User: godd
 * Date: 2017-11-14
 * Time: 20:51
 */
@Slf4j
@Service
public class UserServer implements BaseServer<User> {
    @Autowired
    private UserMapper mapper;

    /**
     * 用户登录,登录成功更新用户最后登录时间
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2018/1/3 23:47
     */
    public User login(User user) {
        user = this.queryOne(user);
        if (user.getId() != null) {
            this.updateLastLoginTime(user.getId());
        }
        return user;
    }

    /**
     * 查询手机号是否被注册
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2018/1/4 00:08
     */
    public boolean findByPhone(String userPhone) {
        User user = new User();
        user.setPhone(userPhone);
        user = this.queryOne(user);
        return user.getId() == null;
    }

    @Override
    public void save(User user) {
        if (user.getId() != null) {
            mapper.updateByPrimaryKeySelective(user);
        } else {
            mapper.insert(user);
        }
    }

    @Override
    public User queryOne(User user) {
        return mapper.selectOne(user);
    }

    public List queryAll(User user) {
        List<User> userList = mapper.select(user);
        return userList;
    }

    @Override
    public int remove(User user) {
        return 0;
    }

    /**
     * 更新最后登录时间
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2018/1/3 23:46
     */
    private int updateLastLoginTime(Integer userId) {
        User updateUser = new User();
        updateUser.setId(userId);
        updateUser.setLastLoginTime(new Date());
        return mapper.updateByPrimaryKeySelective(updateUser);
    }
}
